package com.tomshushu.doc.freedoc.modular.team.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomshushu.doc.freedoc.core.exception.ServiceException;
import com.tomshushu.doc.freedoc.modular.project.entity.ProjectEntity;
import com.tomshushu.doc.freedoc.modular.project.service.ProjectService;
import com.tomshushu.doc.freedoc.modular.team.entity.TeamEntity;
import com.tomshushu.doc.freedoc.modular.team.mapper.TeamMapper;
import com.tomshushu.doc.freedoc.modular.team.model.param.AddTeamParam;
import com.tomshushu.doc.freedoc.modular.team.model.param.DeleteTeamParam;
import com.tomshushu.doc.freedoc.modular.team.model.param.EditTeamParam;
import com.tomshushu.doc.freedoc.modular.team.service.TeamService;
import com.tomshushu.doc.freedoc.modular.teamuser.entity.TeamUserEntity;
import com.tomshushu.doc.freedoc.modular.teamuser.model.param.TeamUserParam;
import com.tomshushu.doc.freedoc.modular.teamuser.service.TeamUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TeamServiceImpl  extends ServiceImpl<TeamMapper, TeamEntity> implements TeamService {

    @Resource
    private TeamUserService teamUserService;
    @Resource
    private ProjectService projectService;

    @Override
    public void addTeam(AddTeamParam addTeamParam) {
        String userId = StpUtil.getLoginIdAsString();
        long count = this.count(new LambdaQueryWrapper<TeamEntity>()
                .eq(TeamEntity::getTeamName, addTeamParam.getTeamName())
                .eq(TeamEntity::getCreateUser, userId));
        if (count > 0) {
            throw new ServiceException(7004, "团队已存在");
        }

        TeamEntity teamEntity = new TeamEntity();
        BeanUtils.copyProperties(addTeamParam, teamEntity);
        teamEntity.setCreateUser(userId);
        save(teamEntity);

        // 当前创建用户加入团队
        TeamUserParam teamUserParam = new TeamUserParam();
        List<TeamUserParam.TeamUser> teamUserList = new ArrayList<>();
        TeamUserParam.TeamUser teamUser = new TeamUserParam.TeamUser();
        teamUser.setTeamId(teamEntity.getTeamId());
        teamUser.setUserId(userId);
        teamUser.setType("OWNER");
        teamUserList.add(teamUser);
        teamUserParam.setTeamUserList(teamUserList);
        teamUserService.addTeamUser(teamUserParam);
    }

    @Override
    public void editTeam(EditTeamParam param) {
        // 根据id查询实体
        TeamEntity teamEntity = this.getById(param.getTeamId());
        if (ObjectUtil.isEmpty(teamEntity)) {
            throw new ServiceException(7005, "团队不存在");
        }
        // 请求参数转化为实体
        BeanUtil.copyProperties(param, teamEntity);
        this.updateById(teamEntity);
    }

    @Override
    public void deleteTeam(DeleteTeamParam param) {

        // 删除团队
        this.removeById(param.getTeamId());
        // 删除团队用户关联关系
        teamUserService.remove(new LambdaQueryWrapper<TeamUserEntity>()
                .eq(TeamUserEntity::getTeamId, param.getTeamId()));

        // 清除项目团队关联关系
        List<ProjectEntity> projectList = projectService.list(new LambdaQueryWrapper<ProjectEntity>()
                .eq(ProjectEntity::getTeamId, param.getTeamId()));
        projectList.forEach(project -> {
            project.setTeamId("");
            projectService.updateById(project);
        });
    }


}